package com.erp.myAdapter


import android.bluetooth.BluetoothDevice
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.erp.R
import com.erp.module.pieceworkGrouping.PieceworkGroupingContract
import kotlinx.android.synthetic.main.item_bluelist.view.bluelistitem_name
import kotlinx.android.synthetic.main.item_bluelist.view.bluelistitem_state
import kotlinx.android.synthetic.main.item_bluelist.view.data

class BlueListAdapter(private val kv : PieceworkGroupingContract.PieceworkGroupingFragmentView, private val mlist: List<BluetoothDevice>) : RecyclerView.Adapter<BlueListAdapter.ViewHolder>() {
    internal var width = 0

    override fun onCreateViewHolder(parent: ViewGroup, p1: Int): BlueListAdapter.ViewHolder {
        val view: View = LayoutInflater.from(parent.context).inflate(R.layout.item_bluelist, parent, false)
        return ViewHolder(view)
    }


    override fun getItemCount(): Int {
        return mlist.size
    }

    override fun onBindViewHolder(h: ViewHolder, p: Int) {
        h.name.text = mlist[p].name;
        h.v.setOnClickListener {
            kv.onClickBlue(p,mlist[p])
        }

        when (mlist[p].bondState){
            BluetoothDevice.BOND_BONDED ->{
                h.state.text = "已连接";
            }
        }
    }

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var name: TextView
        var state: TextView
        var data : ConstraintLayout
        var v: View
        init {
            v = view
            data = view.data
            name = view.bluelistitem_name
            state = view.bluelistitem_state
        }
    }



}
